/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ManageHousesMainPanel.java
 *
 * Created on 18.Kas.2009, 21:39:22
 */

package fairhelper.panels;

import fairhelper.House;
import fairhelper.HouseManager;
import fairhelper.UserManager;
import fairhelper.language.Language;
import fairhelper.theme.Theme;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.text.Position.Bias;

/**
 *
 * @author Feoran
 */
public class ManageHousesMainPanel extends javax.swing.JPanel {

    /** Creates new form ManageHousesMainPanel */
    public ManageHousesMainPanel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        searchHouseField = new javax.swing.JTextField();
        searchHouseButton = new javax.swing.JButton();
        housesListScroll = new javax.swing.JScrollPane();
        housesList = new javax.swing.JList();
        houseInfoPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        houseNameLabel = new javax.swing.JLabel();
        houseIdLabel = new javax.swing.JLabel();
        personNumberLabel = new javax.swing.JLabel();
        helpPointLabel = new javax.swing.JLabel();
        deleteHouseButton = new javax.swing.JButton();
        updateHousePanelButton = new javax.swing.JButton();
        showPeopleButton = new javax.swing.JButton();
        showHouseHelpsButton = new javax.swing.JButton();

        setBackground(Theme.getColor("backgroundColor2"));
        setPreferredSize(new java.awt.Dimension(620, 480));

        searchHouseField.setBackground(Theme.getColor("fieldBackgroundColor1"));
        searchHouseField.setForeground(Theme.getColor("fieldForegroundColor1"));

        searchHouseButton.setBackground(Theme.getColor("buttonBackgroundColor1"));
        searchHouseButton.setForeground(Theme.getColor("buttonForegroundColor1"));
        searchHouseButton.setText("Hane Arama");
        searchHouseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchHouseButtonActionPerformed(evt);
            }
        });

        housesList.setBackground(Theme.getColor("fieldBackgroundColor1"));
        housesList.setForeground(Theme.getColor("fieldForegroundColor1"));
        housesList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = HouseManager.getHouseNameArray();
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        housesList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                housesListValueChanged(evt);
            }
        });
        housesListScroll.setViewportView(housesList);

        houseInfoPanel.setBackground(Theme.getColor("backgroundColor2"));

        jLabel1.setForeground(Theme.getColor("labelColor1"));
        jLabel1.setText(Language.getText("houseNameLabel"));

        jLabel2.setForeground(Theme.getColor("labelColor1"));
        jLabel2.setText(Language.getText("houseIdLabel"));

        jLabel3.setForeground(Theme.getColor("labelColor1"));
        jLabel3.setText(Language.getText("housePersonNumberLabel"));

        jLabel7.setForeground(Theme.getColor("labelColor1"));
        jLabel7.setText(Language.getText("houseHelpPointLabel"));

        houseNameLabel.setForeground(Theme.getColor("labelColor1"));

        houseIdLabel.setForeground(Theme.getColor("labelColor1"));

        personNumberLabel.setForeground(Theme.getColor("labelColor1"));

        helpPointLabel.setForeground(Theme.getColor("labelColor1"));

        deleteHouseButton.setBackground(Theme.getColor("buttonBackgroundColor1"));
        deleteHouseButton.setForeground(Theme.getColor("buttonForegroundColor1"));
        deleteHouseButton.setText("Haneyi Sil");
        deleteHouseButton.setEnabled(!housesList.isSelectionEmpty());
        deleteHouseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteHouseButtonActionPerformed(evt);
            }
        });

        updateHousePanelButton.setBackground(Theme.getColor("buttonBackgroundColor1"));
        updateHousePanelButton.setForeground(Theme.getColor("buttonForegroundColor1"));
        updateHousePanelButton.setText("Haneyi Güncelle");
        updateHousePanelButton.setEnabled(!housesList.isSelectionEmpty());

        showPeopleButton.setBackground(Theme.getColor("buttonBackgroundColor1"));
        showPeopleButton.setForeground(Theme.getColor("buttonForegroundColor1"));
        showPeopleButton.setText("Kişileri Göster");
        showPeopleButton.setEnabled(!housesList.isSelectionEmpty());

        showHouseHelpsButton.setText("Yardımları Göster");
        showHouseHelpsButton.setEnabled(false);

        javax.swing.GroupLayout houseInfoPanelLayout = new javax.swing.GroupLayout(houseInfoPanel);
        houseInfoPanel.setLayout(houseInfoPanelLayout);
        houseInfoPanelLayout.setHorizontalGroup(
            houseInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(houseInfoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(houseInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(houseInfoPanelLayout.createSequentialGroup()
                        .addGroup(houseInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel7))
                        .addGap(40, 40, 40)
                        .addGroup(houseInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(houseNameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
                            .addComponent(houseIdLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
                            .addComponent(personNumberLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
                            .addComponent(helpPointLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)))
                    .addGroup(houseInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(showHouseHelpsButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(showPeopleButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(updateHousePanelButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(deleteHouseButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)))
                .addContainerGap())
        );
        houseInfoPanelLayout.setVerticalGroup(
            houseInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(houseInfoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(houseInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(houseNameLabel))
                .addGap(18, 18, 18)
                .addGroup(houseInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(houseIdLabel))
                .addGap(18, 18, 18)
                .addGroup(houseInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(personNumberLabel))
                .addGap(18, 18, 18)
                .addGroup(houseInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(helpPointLabel))
                .addGap(18, 18, 18)
                .addComponent(deleteHouseButton)
                .addGap(18, 18, 18)
                .addComponent(updateHousePanelButton)
                .addGap(18, 18, 18)
                .addComponent(showPeopleButton)
                .addGap(18, 18, 18)
                .addComponent(showHouseHelpsButton)
                .addContainerGap(26, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(56, 56, 56)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(housesListScroll, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(searchHouseField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(searchHouseButton, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(houseInfoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(48, 48, 48))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(searchHouseField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(searchHouseButton))
                .addGap(41, 41, 41)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(housesListScroll, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(houseInfoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(27, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    public JButton getUpdateHousePanelButton() {
        return updateHousePanelButton;
    }

    public JButton getShowPeopleButton() {
        return showPeopleButton;
    }

    public JButton getShowHouseHelpsButton() {
        return showHouseHelpsButton;
    }

    public House getSelectedHouse() {
        return HouseManager.getHouse(housesList.getSelectedIndex());
    }

    public int getHouseListSelectedIndex() {
        return housesList.getSelectedIndex();
    }
/**
 * girilen bilgiye göre ilgili haneyi bulur
 * @param evt
 */
    private void searchHouseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchHouseButtonActionPerformed
        int index = housesList.getNextMatch(searchHouseField.getText(), 0, Bias.Forward);
        if (index != -1) {
            housesList.setSelectedIndex(index);
            if (housesListScroll.getVerticalScrollBar().getValue() != 0) {
                int listHeight = housesListScroll.getVerticalScrollBar().getMaximum();
                int scrollHeight = housesListScroll.getVerticalScrollBar().getVisibleAmount();
                int newValue = ((listHeight - scrollHeight) / housesList.getLastVisibleIndex()) * housesList.getSelectedIndex();
                housesListScroll.getVerticalScrollBar().setValue(newValue);
            }
        }
        else {
            JOptionPane.showMessageDialog(null, Language.getText("helpCouldntFoundMessage"));
            searchHouseField.setText("");
        }
    }//GEN-LAST:event_searchHouseButtonActionPerformed

    private void housesListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_housesListValueChanged
        if (!housesList.isSelectionEmpty()) {
            House selectedHouse = HouseManager.getHouse(housesList.getSelectedIndex());
            houseNameLabel.setText(selectedHouse.getSurname());
            houseIdLabel.setText(Integer.toString(selectedHouse.getId()));
            personNumberLabel.setText(Integer.toString(selectedHouse.getPersons().size()));
            helpPointLabel.setText(Integer.toString(selectedHouse.getHelpPoint()));
            if (UserManager.getManageHousePrivilage()) {
                deleteHouseButton.setEnabled(true);
                updateHousePanelButton.setEnabled(true);
                showPeopleButton.setEnabled(true);
		    showHouseHelpsButton.setEnabled(true);
            }
            else {
                deleteHouseButton.setEnabled(false);
                updateHousePanelButton.setEnabled(false);
                showPeopleButton.setEnabled(false);
		    showHouseHelpsButton.setEnabled(false);
            }
        }
        else {
            houseNameLabel.setText("");
            houseIdLabel.setText("");
            personNumberLabel.setText("");
            helpPointLabel.setText("");
            deleteHouseButton.setEnabled(false);
            updateHousePanelButton.setEnabled(false);
            showPeopleButton.setEnabled(false);
		showHouseHelpsButton.setEnabled(false);
        }
    }//GEN-LAST:event_housesListValueChanged

    private void deleteHouseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteHouseButtonActionPerformed
        String message = Language.getText("deleteHouseMessage");
        int answer = JOptionPane.showConfirmDialog(null, message, Language.getText("deleteHouseButton"), JOptionPane.YES_NO_OPTION);
        if (answer == JOptionPane.YES_OPTION) {
            House selectedHouse = HouseManager.getHouse(housesList.getSelectedIndex());
            HouseManager.removeHouse(selectedHouse.getId());
            housesList.clearSelection();
            housesList.removeAll();
            housesList.setListData(HouseManager.getHouseNameArray());
            housesList.revalidate();
            housesList.repaint();
            housesListValueChanged(null);
        }
    }//GEN-LAST:event_deleteHouseButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton deleteHouseButton;
    private javax.swing.JLabel helpPointLabel;
    private javax.swing.JLabel houseIdLabel;
    private javax.swing.JPanel houseInfoPanel;
    private javax.swing.JLabel houseNameLabel;
    private javax.swing.JList housesList;
    private javax.swing.JScrollPane housesListScroll;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel personNumberLabel;
    private javax.swing.JButton searchHouseButton;
    private javax.swing.JTextField searchHouseField;
    private javax.swing.JButton showHouseHelpsButton;
    private javax.swing.JButton showPeopleButton;
    private javax.swing.JButton updateHousePanelButton;
    // End of variables declaration//GEN-END:variables

}
